package cn.picclife.mwx.manager.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.picclife.mwx.common.core.util.JsonUtils;
import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.picclife.mwx.common.web.base.BaseResult;
import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.BranchBo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.po.Branch;
import cn.picclife.mwx.manager.sys.service.BranchService;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping(value = "/branch")
public class BranchController {
	
	@Autowired
	private BranchService service;
	
	@ResponseBody
	@RequestMapping(value = "/add_branch")
	public BaseResult1 insertBranch(@RequestBody Branch branch,HttpServletResponse response,HttpServletRequest request){
		BaseResult1 result = new BaseResult1();
		
		int num = service.insertBranch(branch);
		if (num == 2) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("机构已经存在");
			return result;
		}else if(num == 0){
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("添加失败");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("添加成功");
			return result;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_branch")
	public BaseResult1 deleteBranch(@RequestBody Branch branch,HttpServletResponse response){
		
		BaseResult1 result = new BaseResult1();
		int flag = service.deleteBranch(branch);
		if (flag == 1) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("删除成功");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("删除失败");
			return result;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/query_branch_page")
	public PageCount queryBranchPage(HttpServletRequest request, @RequestBody BranchBo branchBo){
		return service.queryBranchPage(branchBo);
	}
	
	@ResponseBody
	@RequestMapping(value = "/query_branch")
	public BaseResult queryBranch(HttpServletRequest request, @RequestBody BranchBo branchBo){
		BaseResult baseResult = new BaseResult(); 
		Branch branch = new Branch();
		try {
        	branch =  service.queryBranch(branchBo.getBranchId());
	        	
		} catch (Exception e) {
//			e.printStackTrace();
			baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("查询失败");	
			return baseResult;
		}
	    baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("查询成功");
        baseResult.setData(branch);    
		return baseResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update_branch")
	public BaseResult1 updateBranch(@RequestBody Branch branch,HttpServletResponse response,HttpServletRequest request){
		BaseResult1 result = new BaseResult1();
		
		int num = service.updateBranch(branch);
		if (num==0) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("修改失败");
			return result;
		}else if (num == 2) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("机构已经存在");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		}
	}

	/**
	 * 查询机构
	 * @param branch
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/queryBranch1")
	public DataResponse queryBranch1(@RequestBody Branch branch, HttpServletRequest request){
		DataResponse dataResponse = new DataResponse();
		List<Branch> list = new ArrayList<>();
		System.out.println("/queryBranch查询机构的请求入参"+ JsonUtils.ObjectToJson(branch));
		list=service.queryBranch1(branch,request);
		dataResponse.setData(list);
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		System.out.println("/queryBranch查询机构的请求回参"+JsonUtils.ObjectToJson(dataResponse));
		return dataResponse;
	}
}
